package com.delu.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.delu.data.Result;
import com.delu.em.ResultEnum;
import com.delu.entity.StorageTable;
import com.delu.entity.StorageVO;
import com.delu.mapper.StorageTableMapper;
import com.delu.utils.IdWorker;
import com.delu.utils.RandomUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 储值卡表(未实名) 前端控制器
 * </p>
 *
 * @author author
 * @since 2021-12-18
 */
@RestController
@Api(value = "储值卡服务接口")
@RequestMapping("/storageTableController")
public class StorageTableController {

    @Autowired
    private StorageTableMapper storageTableMapper;
    @Autowired
    private IdWorker idWorker;

    /**
     * 储值卡生成
     */

    @PostMapping
    @ApiOperation("订单增加方法")
    //paramType:body 表示@RequestBody接受的实体类 path:表示路径/{} 传参  query @RequestParam接收参数
    @ApiImplicitParam(name = "storageTable",value = "储值卡实体类",required = true,paramType = "body")
    @RefreshScope //开启自动刷新配置
    public Result insertStorageTableList(/*@RequestBody StorageTable storageTable*/){
        try {
            for (int i = 0; i < 100; i++) {
                List<String> list = RandomUtil.getUserIds(new ArrayList(), 1);
                for (String s : list) {
                    StorageTable storageTable1 = new StorageTable();
                    storageTable1.setStorageCardId(String.valueOf(idWorker.nextId())).setStorageCardPassword(String.valueOf(s)).setStorageMoney(new BigDecimal(120)).setStorageStatus(1);
                    storageTableMapper.insert(storageTable1);
                }
            }
            return Result.response(ResultEnum.SUCCESS).setData("ok");
        }catch (Exception e){
            e.printStackTrace();
            return Result.response(ResultEnum.ERROR).setData(e);
        }
    }

    /**
     * 储值卡删除(逻辑删除)
     * 储值卡不可充值
     */
    @DeleteMapping("/{storageCardId}")
    @ApiOperation(value = "根据储值卡id删除")
    @ApiImplicitParam(name = "storageCardId",value = "储值卡id",required = true,paramType = "path")
    public Result deleteStorageTableById(@PathVariable(value = "storageCardId",required = true) Integer storageCardId){
        try {
            StorageTable storageTable = storageTableMapper.selectById(storageCardId);
            storageTable.setStorageStatus(0);
            storageTableMapper.updateById(storageTable);
            return Result.response(ResultEnum.SUCCESS).setData("ok");
        }catch (Exception e){
            e.printStackTrace();
            return Result.response(ResultEnum.ERROR).setData(e);
        }
    }

    @GetMapping
    @ApiOperation(value = "根据储值卡id查询")
    @ApiImplicitParam(name = "storageCardId",value = "储值卡id",required = true,paramType = "query")
    public Result findOrderById(@RequestParam("storageCardId") Integer storageCardId) {

        try {
            StorageTable storageTable = storageTableMapper.selectById(storageCardId);
            return Result.response(ResultEnum.SUCCESS).setData(storageTable);
        }catch (Exception e){
            e.printStackTrace();
            return Result.response(ResultEnum.ERROR).setData(e);
        }
    }

    @PostMapping("selectAll")
    @ApiOperation(value = "查询所有储值卡")
    public Result selectStorageTableList(){
        try {
            List<StorageTable> storageTables = storageTableMapper.selectList(null);
            return Result.response(ResultEnum.SUCCESS).setData(storageTables);
        }catch (Exception e){
            e.printStackTrace();
            return Result.response(ResultEnum.ERROR).setData(e);
        }
    }



    /**
     * 储值卡消费
     *
     */
    @PutMapping("paymentStorageTable")
    @ApiOperation(value = "储值卡消费")
    @ApiImplicitParam(name = "storageTable",value = "储值卡与金额实体VO类",required = true,paramType = "body")
    public Result paymentStorageTable(@RequestBody StorageVO storageVO){
        try {
            StorageVO storageVO1 = JSONObject.parseObject(JSON.toJSONString(storageVO), StorageVO.class);
            StorageTable storageTable = storageVO1.getStorageTable();
            BigDecimal rentPrice = storageVO1.getRentPrice();
            //1.通过用户输入的卡号卡密来进行查询乐享卡余额  商品金额
            StorageTable storageTable1 = storageTableMapper.selectById(storageTable.getStorageCardId());
            //判断储值卡是否可用
            if(storageTable1.getStorageStatus() != 1){
                return Result.response(ResultEnum.NOT_CARD);
            }
            //判断卡号是否正确
            if (storageTable1 == null || "".equals(storageTable)){
                return Result.response(ResultEnum.ERRORPWD_CARD).setData(null);
            }
            //判断密码
            if(!storageTable.getStorageCardPassword().equals(storageTable1.getStorageCardPassword())){
                System.out.println(storageTable.getStorageCardPassword());
                System.out.println(storageTable1.getStorageCardPassword());
                return Result.response(ResultEnum.ERRORPWD).setData(null);
            }

            //判断余额是否充足
            if(rentPrice.compareTo(storageTable1.getStorageMoney()) < 1){//前端传过来的金额  小于等于 表中金额
                //执行减金额操作
                BigDecimal money = storageTable1.getStorageMoney().subtract(rentPrice);
                if (money.compareTo(new BigDecimal(0)) == 0){//如果减完后金额为0的话  修改状态
                    storageTable1.setStorageStatus(0).setStorageMoney(new BigDecimal(0));//无效
                    storageTableMapper.updateById(storageTable1);
                    //执行添加消费记录操作


                    return Result.response(ResultEnum.PAYMENT_OK).setData("null");
                }
                storageTable1.setStorageMoney(money);
                storageTableMapper.updateById(storageTable1);
                return Result.response(ResultEnum.PAYMENT_OK).setData("null");

            }

            return Result.response(ResultEnum.BALANCE).setData("null");
        }catch (Exception e){
            e.printStackTrace();
            return Result.response(ResultEnum.ERROR).setData(e);
        }
    }


}
